﻿using EasyToHelp.Common;
using EasyToHelp.DBFactory;
using EasyToHelp.Entity;
using System.Collections.Generic;
using System.Reflection;

namespace EasyToHelp.Service
{
    public class CommonService
    {
        #region 获取任意表数据
        /// <summary>
        /// 获取任意表数据
        /// </summary>
        /// <param name="TableName">表名</param>
        /// <param name="PkName">查询键</param>
        /// <param name="PkVaule">查询键值</param>
        /// <returns></returns>
        //public object GetDataTable(string TableName, string PkName, string PkVaule)
        //{
        //    string sql = string.Format("select *  from {0} where {1}='{2}'", TableName, PkName, PkVaule);
        //    return JsonHelper.SerializeObject(DBHelper.GetDataTable(sql));
        //}
        #endregion

        #region 查询表配置信息
        /// <summary>
        /// 查询配置信息
        /// </summary>
        /// <param name="TableName"></param>
        /// <returns></returns>
        public static List<TableManageField> GetTableManageFieldList(string TableName)
        {
            return DBHelper.GetList<TableManageField>();
        }
        //public static object GetTableManageField(string EnglishName)
        //{
        //    string sql = string.Format("SELECT 	*  FROM TableManage_Field WHERE EnglishName='{0}'", EnglishName);
        //    return JsonHelper.SerializeObject(DBHelper.GetDataTable(sql));
        //}
        #endregion

        #region 获取表信息
        /// <summary>
        /// 获取表信息
        /// </summary>
        /// <param name="TableName"></param>
        /// <returns>返回表字段名、字段类型、字段长度</returns>
        public static IEnumerable<TableInfo> GetTableInfo(string TableName)
        {
            var sqlSyscolumns = "select a.name as [column],b.name as type,a.length as length from syscolumns a,systypes b where a.id=object_id('" + TableName + "') and a.xtype=b.xtype";
            return DBHelper.GetList<TableInfo>(sqlSyscolumns);
        }
        #endregion
    }
}
